package com.example.android_swt.jnidemo;

import org.bouncycastle.util.encoders.Base64;

/**
 * Base64的加密和解密
 */
public class Base64Utils {


    /**
     * 将 value 进行 BASE64 编码
     *
     * @param value
     * @return
     */
    public static String getEncryptionBASE64(String value) {
        if (value == null) {
            return null;
        }
        return Base64.toBase64String(value.getBytes());
    }

    /**
     * 将 BASE64 编码的字符串 value 进行解码
     *
     * @param value
     * @return
     */
    public static String getDecryptFromBASE64(String value) {
        if (value == null) {
            return null;
        }
        try {
            byte[] b = Base64.decode(value);
            return new String(b);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String publicKey = "tiamaesoft";
        String jiami = Base64Utils.getEncryptionBASE64(publicKey);
        System.out.println("加密字符串=="+jiami);
        String jiemi = Base64Utils.getDecryptFromBASE64(jiami);
        System.out.println("解密字符串=="+jiemi);
        if (jiemi.equals(publicKey)) {
            System.out.println("加解密正常");
        } else {
            System.out.println("加解密失败");
        }
    }


}